﻿//通用shown自动调用些方式
function FormShown() {
    mini.parse();
    var tree = mini.get("tree1");
    if (tree != undefined) {
        tree.on("nodeselect", function (e) {
            LoadListData();
        });
    }
}

var FCorpShow = "";
//加载资料
function SetData(data) {
    var grid = mini.get("datagrid1");
    mini.getbyName("FTbName").setValue(data.sTbName);
    try{
        mini.getbyName("FClassID").setValue(data.ClassID);
    } catch (e) {
    }
    mini.getbyName("FSearchKey").setValue(data.sSearchKey);
    FCorpShow = data.FCorpShow;
    LoadListTree();
}

//加载分类树
function LoadListTree() {
    var sTName = mini.getbyName("FTbName").getValue();
    var tree = mini.get("tree1");
    if (tree == undefined)
        return;
    $.post('GetItemTree', { mSystemAppID: parent.window.getSystemAppID(), mTableCode: sTName }, function (data) {
        tree.loadList(mini.decode(data));
        tree.select(0);
    });
}

//加载资料
function search() {
    LoadListData();
}


//加载用户列表
function LoadListData() {
    var sTbName = mini.getbyName("FTbName").getValue();
    if (sTbName == '') return;
    var tree = mini.get("tree1");
    if (tree == undefined) {
        var node = tree.getSelectedNode();
        var classid = node.FItemID;
        if(mini.getbyName("FClassID").getValue()!='')
            classid = mini.getbyName("FClassID").getValue();
        var json = "[{";
        json = json + "'FTbName':" + "'" + sTbName + "',";
        json = json + "'FClassID':" + "'"+classid+"',";
        json = json + "'FNumber':" + "'" + mini.getbyName("FSearchKey").getValue() + "',";
        json = json + "'FSystemAppID': '" + parent.window.getSystemAppID() + "'}]";
        var grid = mini.get("datagrid1");
        grid.on("load", function (data) {
            var index = 0;
            $.each(data.data, function (index, data) {
                if (FCorpShow != undefined)
                    if (FCorpShow.indexOf(data.FItemID) > 0)
                        grid.select(index);
                index++;
            });
        });
        grid.load({ datajson: json });
        return;
    }

    var mload = tree.getData();
    if (mload.length > 0) {
        var node = tree.getSelectedNode();
        var classid = node.FItemID;
        if (mini.getbyName("FClassID").getValue() != '')
            classid = mini.getbyName("FClassID").getValue();
        var json = "[{";
        json = json + "'FTbName':" + "'" + sTbName + "',";
        json = json + "'FClassID':" + "'" + classid + "',";
        json = json + "'FNumber':" + "'" + mini.getbyName("FSearchKey").getValue() + "',";
        json = json + "'FSystemAppID': '" + parent.window.getSystemAppID() + "'}]";
        var grid = mini.get("datagrid1");
        grid.on("load", function (data) {
            var index = 0;
            $.each(data.data, function (index, data) {
                if(FCorpShow!=undefined)
                if (FCorpShow.indexOf(data.FItemID) > 0)
                    grid.select(index);
                index++;
            });
        });
        grid.load({ datajson: json });
    }
}

function LoadData() {
}

function GetAllSelecteds() {
    var data = [];
    for (var pageIndex in selectMaps) {
        var rows = selectMaps[pageIndex];
        data.addRange(rows);
    }
    return data;
}

var selectMaps = {};
function onSelectoinChanged(e) {
    var grid = mini.get("datagrid1");
    var rows = grid.getSelecteds();
    selectMaps[grid.getPageIndex()] = rows;
}

function onSelectOne(e) {
    var grid = mini.get("datagrid1");
    var rec = grid.getSelecteds();
    if (rec) {
        CloseWindow(rec);
    }
}

function onSelectedNodes(e) {
    CloseWindow(GetAllSelecteds(selectMaps));
    
}